package com.google.android.location.content;



import com.google.android.locatiom.maps.myroutes.R;
import com.google.android.location.content.libcontent.Route;
import com.google.android.location.content.libcontent.RoutesColumns;
import com.google.android.location.content.libdata.MyRouteProvider;

import android.app.Activity;
import android.content.ContentValues;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;

public class RouteDetails extends Activity implements OnClickListener
{

	  /**
	   * The id of the track being edited (taken from bundle, "routeid")
	   */
	  private Long routeId;

	  private EditText name;
	  private EditText description;
	  private AutoCompleteTextView category;

	  @Override
	  protected void onCreate(Bundle bundle) {
	    super.onCreate(bundle);
	    setContentView(R.layout.myroutes_details);

	    // Required extra when launching this intent:
	    routeId = getIntent().getLongExtra("routeid", -1);
	    if (routeId < 0) {
	      Log.d(Constants.TAG,
	          "RouteDetails intent was launched w/o track id.");
	      finish();
	      return;
	    }

	    // Optional extra that can be used to suppress the cancel button:
	    boolean hasCancelButton =
	        getIntent().getBooleanExtra("hasCancelButton", true);

	    name = (EditText) findViewById(R.id.routedetails_name);
	    description = (EditText) findViewById(R.id.routedetails_description);
	    category = (AutoCompleteTextView) findViewById(R.id.routedetails_category);
	    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
	        this,
	        R.array.activity_types,
	        android.R.layout.simple_dropdown_item_1line);
	    category.setAdapter(adapter);

	    Button cancel = (Button) findViewById(R.id.routedetails_cancel);
	    if (hasCancelButton) {
	      cancel.setOnClickListener(this);
	      cancel.setVisibility(View.VISIBLE);
	    } else {
	      cancel.setVisibility(View.INVISIBLE);
	    }
	    Button save = (Button) findViewById(R.id.routedetails_save);
	    save.setOnClickListener(this);

	    fillDialog();
	  }

	  private void fillDialog() {
	    Route track = MyRouteProvider.Factory.getRouteProvider(this).getRouteById(routeId);
	    if (track != null) {
	      name.setText(track.getName());
	      description.setText(track.getDescription());
	      category.setText(track.getCategory());
	    }
	  }

	  private void saveDialog() {
	    ContentValues values = new ContentValues();
	    values.put(RoutesColumns.NAME, name.getText().toString());
	    values.put(RoutesColumns.DESCRIPTION, description.getText().toString());
	    values.put(RoutesColumns.CATEGORY, category.getText().toString());
	    getContentResolver().update(
	    		RoutesColumns.CONTENT_URI,
	        values,
	        "_id = " + routeId,
	        null/*selectionArgs*/);
	  }

	  @Override
	  public void onClick(View v) {
	    switch (v.getId()) {
	      case R.id.routedetails_cancel:
	        finish();
	        break;
	      case R.id.routedetails_save:
	        saveDialog();
	        finish();
	        break;
	    }
	  }
	}
